package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import com.itheima.service.SetmealService;
import jdk.nashorn.internal.ir.Flags;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderlist")
public class OrderListController {

    @Reference
    private OrderService orderService;

    @Reference
    private SetmealService setmealService;


    /**
     * 添加订单
     * @param map
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Map map)  {
        map.put("orderType", Order.ORDERTYPE_TELEPHONE);
        try {
            Result result = orderService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDERLIST_FAIL);
        }
    }

    /**
     * 根据页码查找订单
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryPageBean.setQueryString(queryPageBean.getQueryString().trim());// 去除查询条件首尾空格
        }
        return orderService.findByPage(queryPageBean);
    }

    /**
     * 通过id查找订单
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    /**
     * 编辑订单
     * @param order
     * @return
     * @throws Exception
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody Order order) throws Exception {
        return orderService.edit(order);
    }

    /**
     * 删除订单
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    public Result delete(Integer id ) throws Exception {
        return orderService.delete(id);
    }


    /**
     * 用于弹出编辑框 ,返回对应id的订单 和 所有的体检套餐
     * @param id
     * @return
     */
    @RequestMapping("findByIdWithAllSetmeal")
    public Result findByIdWithAllSetmeal(Integer id) {
        try {
            Map map = orderService.findById(id);
            List<Setmeal> setmeals = setmealService.findAll();
            Map data = new HashMap();
            data.put("map", map);
            data.put("setmeals", setmeals);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }
}
